﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BusinessLogic;

namespace Interface.Houses
{
    public partial class District : System.Web.UI.Page
    {
        DataValid dv = new DataValid();
        DataConnection dc = new DataConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Page.IsPostBack == false)
            {
                bindToGrid();
                loadCity();
            }
        }

        protected void loadCity()
        {
            string strsql = "Select Name from City";
            DataTable dt = dc.getData(strsql);
            dlCity.Items.Add(string.Empty);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    string CityName = row["Name"].ToString();

                    dlCity.Items.Add(CityName);
                }
            }

        }

        protected void bindToGrid()
        {         
            string strsql = "Select * from District";
            DataTable dt = dc.getData(strsql);
            DataTable dtUser = dataTableInitiate();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["IdDistrict"].ToString());

                    int _cityId = Convert.ToInt32(row["IdCity"]);

                    string _cityName = dc.getCityName(_cityId);

                    string _disName = row["Name"].ToString();
                    
                    DataRow dtRow = dtUser.NewRow();
                    dtRow["ID"] = ID;
                    dtRow["STT"] = stt;
                    dtRow["City"] = _cityName;
                    dtRow["District"] = _disName;
                    
                    dtUser.Rows.Add(dtRow);
                }
            }

            
            GridDistrict.DataSource = dtUser;
            GridDistrict.DataBind();
        }

        protected DataTable dataTableInitiate()
        {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;
            dt.Columns.Add("STT", typeof(string));
            dt.Columns.Add("City", typeof(string));
            dt.Columns.Add("District", typeof(string));
            
            return dt;
        }

        protected void btAddDis_Click(object sender, EventArgs e)
        {
            string CityName = dlCity.Text;
            string _cityName = dv.checktext(CityName);
            if (_cityName == string.Empty)
            {
                string strsql = "Select IdCity from City where Name = N'" + CityName + "' ";
                int IdCity = dc.getNumberValue(strsql);

                string NameDis = txtDistrict.Text;
                string _checked = dv.checktext(NameDis);
                if (_checked == string.Empty)
                {
                    bool check = dc.checkDis(NameDis);
                    if (check == false)
                    {
                        lbError.Text = "Quận đã tồn tại";
                        return;
                    }

                    else
                    {

                        string insertQuery = "insert into District values ('" + IdCity + "',N'" + NameDis + "')";
                        dc.ExecNonQuery(insertQuery);
                        bindToGrid();
                    }

                }
                else
                {
                    lbError.Text = _checked;
                }
            }else
            {
                lbError.Text = "Bạn phải chọn thành phố";
            }
            
        }

        protected override void OnInit(EventArgs e)
        {
            GridDistrict.PageIndexChanging += new GridViewPageEventHandler(GridDistrict_PageIndexChanging);
            GridDistrict.RowEditing += new GridViewEditEventHandler(GridDistrict_RowEditing);
            GridDistrict.RowUpdating += new GridViewUpdateEventHandler(GridDistrict_RowUpdating);
            GridDistrict.RowCancelingEdit += new GridViewCancelEditEventHandler(GridDistrict_RowCancelingEdit);
            GridDistrict.RowDataBound += new GridViewRowEventHandler(GridDistrict_RowDataBound);

        }

        void GridDistrict_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView drv = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    TextBox txt = (TextBox)e.Row.FindControl("txtEditDisName");
                    txt.Text = DataBinder.Eval(e.Row.DataItem, "District").ToString();
                    
                }
            }
        }

        void GridDistrict_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridDistrict.EditIndex = -1;
            bindToGrid();
        }

        void GridDistrict_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            

            int id = (int)GridDistrict.DataKeys[e.RowIndex].Value;

            TextBox tx1 = (TextBox)GridDistrict.Rows[e.RowIndex].FindControl("txtEditDisName");

            
            string Dis = tx1.Text;

            if (Dis == String.Empty)
            {
                lbError.Text = "Bạn phải nhập vào tên của quận";
                return;

            }
            else
            {
                Boolean _checked = dc.checkDis(Dis);
                if (_checked == true)
                {
                    string updateQuery = "update District Set Name = N'" + Dis + "' where idDistrict = '"+ id +"'";
                    dc.ExecNonQuery(updateQuery);
                    bindToGrid();
                    //UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value

                    //"(update District Set Name = '"+ Dis +"' where id = '"+ id +"')"
                }
                else
                {
                    lbError.Text = "Quận đã tồn tại";
                    return;
                }
            }

            GridDistrict.EditIndex = -1;
            bindToGrid();
        }

        void GridDistrict_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridDistrict.EditIndex = e.NewEditIndex;
            bindToGrid();

            


            
        }

       

        void GridDistrict_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridDistrict.PageIndex = e.NewPageIndex;
            //loadUser();
        }
}
}